﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;

namespace QLBN
{
    class BenhNhan
    {
        DBConnector dbc = new DBConnector();
        public static Boolean IsClose = false;
        public static Boolean cksuadl = false;


        public static string MBN;

        String _mabn;

        public String Mabn
        {
            get { return _mabn; }
            set { _mabn = value; }
        }
        String _tenbn;

        public String Tenbn
        {
            get { return _tenbn; }
            set { _tenbn = value; }
        }
        String _tuoibn;

        public String Tuoibn
        {
            get { return _tuoibn; }
            set { _tuoibn = value; }
        }
        String _cannang;

        public String Cannang
        {
            get { return _cannang; }
            set { _cannang = value; }
        }
        String _diachi;

        public String Diachi
        {
            get { return _diachi; }
            set { _diachi = value; }
        }
        String _benhsu;

        public String Benhsu
        {
            get { return _benhsu; }
            set { _benhsu = value; }
        }
        String _diung;

        public String Diung
        {
            get { return _diung; }
            set { _diung = value; }
        }
        String _khangthuoc;

        public String Khangthuoc
        {
            get { return _khangthuoc; }
            set { _khangthuoc = value; }
        }
        String _ghichu;

        public String Ghichu
        {
            get { return _ghichu; }
            set { _ghichu = value; }
        }
        String _tot;

        public String Tot
        {
            get { return _tot; }
            set { _tot = value; }
        }
        String _vua;

        public String Vua
        {
            get { return _vua; }
            set { _vua = value; }
        }
        String _khong;

        public String Khong
        {
            get { return _khong; }
            set { _khong = value; }
        }
        String _lanmot;

        public String Lanmot
        {
            get { return _lanmot; }
            set { _lanmot = value; }
        }
        String _lanhai;

        public String Lanhai
        {
            get { return _lanhai; }
            set { _lanhai = value; }
        }
        String _lanba;

        public String Lanba
        {
            get { return _lanba; }
            set { _lanba = value; }
        }

        String _tenThuoc;

        public String TenThuoc
        {
            get { return _tenThuoc; }
            set { _tenThuoc = value; }
        }
        String _lieuDung;

        public String LieuDung
        {
            get { return _lieuDung; }
            set { _lieuDung = value; }
        }
        String _thoiGian;

        public String ThoiGian
        {
            get { return _thoiGian; }
            set { _thoiGian = value; }
        }
        String _ghiChuToa;

        public String GhiChuToa
        {
            get { return _ghiChuToa; }
            set { _ghiChuToa = value; }
        }
        String _maCTT;

        public String MaCTT
        {
            get { return _maCTT; }
            set { _maCTT = value; }
        }
        String _maToa;

        public String MaToa
        {
            get { return _maToa; }
            set { _maToa = value; }
        }
        String _ngayKham;

        public String NgayKham
        {
            get { return _ngayKham; }
            set { _ngayKham = value; }
        }

        private String _phMot;

        public String PhMot
        {
            get { return _phMot; }
            set { _phMot = value; }
        }
        private String _phHai;

        public String PhHai
        {
            get { return _phHai; }
            set { _phHai = value; }
        }
        private String _phBa;

        public String PhBa
        {
            get { return _phBa; }
            set { _phBa = value; }
        }

        internal DataSet Lay_Thong_Tin_Benh_Nhan(String tenbn)
        {
            DataSet ds = null;
            String sql = "Select * from BenhNhan where TenBN = '" + tenbn + "'";
            ds = dbc.executeQuery(sql);
            return ds;
        }

        internal DataSet Lay_Thong_Tin_Benh_Nhan()
        {
            DataSet ds = null;
            String sql = "Select * from BenhNhan";
            ds = dbc.executeQuery(sql);
            return ds;
        }

        internal DataSet Danh_Sach_Benh_Nhan()
        {
            DataSet ds = null;
            String sql = "Select Top 35 MaBN,TenBN,TuoiBN,CanNang,DiaChi from BenhNhan";
            ds = dbc.executeQuery(sql);
            return ds;
        }

        internal DataSet Danh_Sach_Benh_Nhan_Full()
        {
            DataSet ds = null;
            String sql = "Select MaBN,TenBN,TuoiBN,CanNang,DiaChi from BenhNhan";
            
            ds = dbc.executeQuery(sql);
            return ds;
        }

        internal DataSet Danh_Sach_Benh_Nhan(String tenbn)
        {
            DataSet ds = null;
            String sql = "Select Top 35 MaBN,TenBN,TuoiBN,CanNang,DiaChi from BenhNhan ";
            //sql += "where TenBN REGEXP BINARY '^" + tenbn + "'";
            //sql += "where MATCH(TenBN) AGAINST(nguyen)";
            sql += "where TenBN like '%" + tenbn + "%'";
            ds = dbc.executeQuery(sql);
            return ds;
        }

        internal DataSet Tim_Benh_Nhan(String tenbn)
        {
            DataSet ds = null;
            String sql = "Select MaBN,TenBN from BenhNhan where TenBN like '" + tenbn + "%'";
            ds = dbc.executeQuery(sql);
            return ds;
        }

        internal Boolean Cap_Nhat_Thong_Tin()
        {
            if (_mabn != "")
            {
                //SQL Update thong tin ca nhan cua benh nhan
                String sql = "Update BenhNhan set ";
                sql += "TenBN = '" + _tenbn + "', " + "TuoiBN = '" + _tuoibn + "', ";
                sql += "CanNang = " + _cannang + ", " + "DiaChi = '" + _diachi + "', ";
                sql += "BenhSu = '" + _benhsu + "', " + "DiUng = '" + _diung + "', ";
                sql += "KhangThuoc = '" + _khangthuoc + "', " + "GhiChu = '" + _ghichu + "' ";
                sql += "where MaBN = " + _mabn ;

                //SQL kiem tra, insert,update DapUng
                String str = "Select * from DapUng where MaBN = " + _mabn;
                DataSet ds = dbc.executeQuery(str);
                if (ds.Tables[0].Rows.Count <= 0)
                {
                    str = "Insert into DapUng (MaBN) values('" + _mabn + "')";
                    dbc.executeNonQuery(str);
                }
                String sql1 = "Update DapUng set ";
                sql1 += "Tot = '" + _tot + "'," + "Vua = '" + _vua + "'," + "Khong = '" + _khong + "' ";
                sql1 += "where MaBN = " + _mabn;

                //SQL kiem tra, insert,update PhoiHop
                str = "Select * from PhoiHop where MaBN = " + _mabn;
                if (ds.Tables[0].Rows.Count <= 0)
                {
                    str = "Insert into PhoiHop (MaBN) values('" + _mabn + "')";
                    dbc.executeNonQuery(str);
                }
                String sql2 = "Update PhoiHop set ";
                sql2 += "PHMot = '" + _phMot + "'," + "PHHai = '" + _phHai + "'," + "PHBa = '" + _phBa + "' ";
                sql2 += "where MaBN = " + _mabn;
                try
                {
                    dbc.executeNonQuery(sql);
                    dbc.executeNonQuery(sql1);
                    dbc.executeNonQuery(sql2);
                    return true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            else
            {
                String sql = "Insert into BenhNhan (TenBN,TuoiBN,CanNang,DiaChi,BenhSu,DiUng,KhangThuoc,GhiChu) ";
                sql += "values ('" + _tenbn + "','" + _tuoibn + "','";
                sql += _cannang + "','" + _diachi + "','" + _benhsu + "','" + _diung + "','";
                sql += _khangthuoc + "','" + _ghichu + "')";

                
                dbc.executeNonQuery(sql);
                return true;
            }
            return false;
                //sql = "Update NguoiDung set UserName='" + username + "' where UserName='" + _username + "'";
        
        }
        internal DataSet Lay_Danh_Sach_Toa(String mabn)
        {
            //String sql = "SELECT ToaThuoc.MaToa, MaCTT,NgayKham ";
            //sql += "FROM ToaThuoc INNER JOIN ChiTietToaThuoc ON ToaThuoc.MaToa = ChiTietToaThuoc.MaToa ";
            //sql += "WHERE ChiTietToaThuoc.MaToa  IN (Select DISTINCT ToaThuoc.MaToa FROM ToaThuoc WHERE MaBN =" + mabn + ")";
            String sql = "SELECT MaToa,NgayKham FROM ToaThuoc WHERE MaBN = " + mabn ;
            DataSet ds = dbc.executeQuery(sql);
            return ds;
        }
        internal DataSet Lay_Danh_Sach_Chi_Tiet_Toa(String maToa)
        {
            DataSet ds;
            String sql = "Select TenThuoc,LieuDung,ThoiGian,GhiChu,MaCTT ";
            sql += "From ChiTietToaThuoc Where MaToa = " + maToa;
            ds = dbc.executeQuery(sql);
            return ds;
        }
        internal Boolean Cap_Nhat_Toa_Thuoc(String maCTT)
        {
            try
            {
                String sql = "Update ChiTietToaThuoc Set ";
                sql += "TenThuoc = '" + _tenThuoc + "', " + "LieuDung = '" + _lieuDung + "', ";
                sql += "ThoiGian = '" + _thoiGian + "', " + "GhiChu = '" + _ghiChuToa + "' ";
                sql += "Where MaCTT = " + maCTT;
                dbc.executeNonQuery(sql);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
            
            return false;
        }
        internal Boolean Xoa_Chi_Tiet_Toa_Thuoc(String maCTT)
        {
            try
            {
                String sql = "Delete From ChiTietToaThuoc Where MaCTT = " + maCTT;
                dbc.executeNonQuery(sql);
                return true;
            }
            catch(Exception ex)
            {
                return false;
            }
            return false;
        }
        internal Boolean Them_Chi_Tiet_Toa()
        {
            try
            {
                String sql = "Insert into ChiTietToaThuoc (MaToa,TenThuoc,LieuDung,ThoiGian,GhiChu) ";
                sql += "values ('" + _maToa + "','" + _tenThuoc + "','";
                sql += _lieuDung + "','" + _thoiGian + "','" + _ghiChuToa + "')";
                dbc.executeNonQuery(sql);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        internal Boolean Them_Toa_Thuoc()
        {
            try
            {
                String sql = "Insert into ToaThuoc (MaBN,NgayKham) ";
                sql += "values ('" + _mabn + "','" + _ngayKham + "')";
                dbc.executeNonQuery(sql);
                //sql = "Select Max(MaToa) From ToaThuoc";
                //String maToa = Convert.ToString(dbc.executeScalar(sql));
                //sql = "Insert into ChiTietToaThuoc (MaToa,TenThuoc,LieuDung,ThoiGian,GhiChu) ";
                //sql += "values ('" + maToa + "','" + _tenThuoc + "','";
                //sql += _lieuDung + "','" + _thoiGian + "','" + _ghiChuToa + "')";
                //dbc.executeNonQuery(sql);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }

        }
        internal Boolean Cap_Nhat_Khang_Thuoc(String maBenhNhan)
        {
            try
            {
                String sql = "Update BenhNhan Set ";
                sql += "KhangThuoc = '" + _khangthuoc + "' ";
                sql += "Where MaBN = " + maBenhNhan;
                dbc.executeNonQuery(sql);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }

        }
        internal Boolean Cap_Nhat_Toa(String maToaThuoc)
        {
            try
            {
                String sql = "Update ToaThuoc Set ";
                sql += "NgayKham = '" + _ngayKham + "' ";
                sql += "Where MaToa = " + maToaThuoc;
                dbc.executeNonQuery(sql);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
